﻿--[[
    @Brief    登录错误提示
    @Author    jameszhai
    @Date    2017-05-17
--]]

ELoginErrorUI = 
{
    sPnlError  = "pnlError";

    sLblTitle  = "lblTitle";
    sRtbTips   = "rtbTips";
    sBtnRetry  = "btnRetry";
    sBtnReload = "btnReload";
    sBtnExit   = "btnExit";
    sFeedBackTips = "richTxtFeedBack";

};

TLoginError = 
{
    InitView = function(nRootID)
        TLoginError.dwRootID = nRootID;
        event_center.add_listener(login_event.LOGIN_FB_NOT_BIND, 
                                  "TLoginError.OnShowFBBind");
        comm_center.reg_service("get_wegame_nobind_info", TLoginError.on_get_wegame_nobind_info);
    end;

    ShowView = function()
        if (not TLoginError.CheckUI()) then
            TGPLog.LogE("LoginE", "Create UI fail.");
            return;
        end;

        if nil ~= TLoginError.pPnlError then
            TLoginError.pPnlError:Show(TRUE);
        else
            TGPLog.LogE("Error", "Error panel is null.");
        end;
    end;

    HideView = function()
        if nil ~= TLoginError.pPnlError then
            TLoginError.pPnlError:Show(FALSE);
        else
            TGPLog.LogE("Error", "Error panel is null.");
        end;
    end;

    CheckUI = function()
        if nil ~= TLoginError.pPnlError then
            return true;
        end;

        TLoginError.pPnlError = ui_utility.get_ctrl_ptr_by_path(TLoginError.dwRootID, 
                                                                ELoginErrorUI.sPnlError);
        if nil == TLoginError.pPnlError then
            TGPLog.LogE("LoginE", "Login panel is null.");
            return false;
        end;

        local     nRoot = TLoginError.pPnlError:GetControlID();

        TLoginError.pLblTitle  = ui_utility.get_ctrl_ptr_by_path(nRoot, ELoginErrorUI.sLblTitle);
        if nil == TLoginError.pLblTitle then
            TGPLog.LogE("LoginE", "Title control is null.");
            return false;
        end;

        TLoginError.pEdtError  = ui_utility.get_ctrl_ptr_by_path(nRoot, ELoginErrorUI.sRtbTips);
        if nil == TLoginError.pEdtError then
            TGPLog.LogE("LoginE", "Text edit is null.");
            return false;
        end;

        TLoginError.pBtnReload = ui_utility.get_ctrl_ptr_by_path(nRoot, ELoginErrorUI.sBtnReload);
        if nil == TLoginError.pBtnReload then
            TGPLog.LogE("LoginE", "Reload button is null.");
            return false;
        end;

        TLoginError.pBtnRetry = ui_utility.get_ctrl_ptr_by_path(nRoot, ELoginErrorUI.sBtnRetry);
        if nil == TLoginError.pBtnRetry then
            TGPLog.LogE("LoginE", "Retry button is null.");
            return false;
        end;

        TLoginError.pBtnExitWG = ui_utility.get_ctrl_ptr_by_path(nRoot, ELoginErrorUI.sBtnExit);
        if nil == TLoginError.pBtnExitWG then
            TGPLog.LogE("LoginE", "Exit button is null.");
            return false;
        end;

        TLoginError.pFeedBackTips = ui_utility.get_ctrl_ptr_by_path(nRoot, 
            ELoginErrorUI.sFeedBackTips);
        if nil == TLoginError.pFeedBackTips then
            TGPLog.LogE("LoginE", "feedback tips rich text is null.");
        else
            ui_utility.setproperty_by_id(TLoginError.pFeedBackTips:GetControlID(), 
                "OnRTBClickHyperLink", "TLoginError.OnClkFeedBack");
        end;

        ui_utility.setproperty_by_id(TLoginError.pBtnReload:GetControlID(), 
                "OnClick", "TLoginError.OnClkReload");
        ui_utility.setproperty_by_id(TLoginError.pBtnRetry:GetControlID(), 
                "OnClick", "TLoginError.OnClkRetry");
        ui_utility.setproperty_by_id(TLoginError.pBtnExitWG:GetControlID(), 
                "OnClick", "TLoginError.OnClkExit");

        return true;
    end;

    UnInitView = function()
        TLoginError.dwRootID  = 0;
        TLoginError.pPnlError = nil;
    end;

    -- 设置状态
    SetStatus = function(zStatus)
        if nil == zStatus then
            TGPLog.LogE("LoginE", "Login error, set status, param is null.");
            return;
        end;

        local     sTitle = LoadStr(ID_LOGIN_LOGIN_FAILED);
        local     sError = zStatus.err_msg;

        if nil ~= zStatus.err_code then
             sError = sError .. "<b>"..LoadStr(ID_LOGIN_ERROR_CODE)..":" .. tostring(zStatus.err_code);
        end;

        if nil ~= zStatus.err_code then
            -- 离线登录
            TLoginError.nOffline = nil;
            if (7610001 == zStatus.err_code) and TLoginModule.IsOfflineEnable() then            -- -- 网络错误 & 离线登录开启
               local ret_tbl = comm_center.call_service("Svr_GetAccountIdOffline", {});
               if ret_tbl ~= nil and ret_tbl.account_id ~= nil and ret_tbl.account_id ~= 0 and ret_tbl.account_id ~= "" then
                   if TLoginModule.IsLoginedAccount(tonumber(ret_tbl.account_id)) then      -- 账号已经登录
                      TLoginError.nOffline = tonumber(ret_tbl.account_id);
                   else
                      local more_error_text = LoadStr("ID_LOGIN_OFFLINE_NO_TICKET");
                      sError = sError .. more_error_text;
                      TGPLog.LogI("LoginE", "IsLoginedAccount return false.");
                   end
               else 
                   if ret_tbl ~= nil then
                       if ret_tbl.account_id == nil or ret_tbl.account_id == 0 or
                          ret_tbl.account_id == "" then
                            local more_error_text = LoadStr("ID_LOGIN_OFFLINE_NO_TICKET");
                            sError = sError .. more_error_text;
                       end
                    end
                   TGPLog.LogI("LoginE", "not get valid account_id by Svr_GetAccountIdOffline.");
               end
            end;

            -- 安全问题，禁止登录，不显示重试按钮
            if (97 == zStatus.err_code) then
                TGPLog.LogI("LoginE", "Error code 97, do not show login error.");
                TLoginError.ShowError(sTitle, sError, false);
                return;
            end;

            if (7610429 == zStatus.err_code) then
                TLoginError.wegame_id = zStatus.wegame_id;
                TLoginError.bind_ticket = zStatus.bind_ticket;
                sTitle = LoadStr("LOGIN_FB_NOTBIND_ERR_TITLE");
                event_center.post_event(login_event.LOGIN_FB_NOT_BIND, "", true);
            end;
        end;
        TLoginError.ShowError(sTitle, sError, true);
    end;

    OnShowFBBind = function(nEvtID, sJson)
        comm_center.call_service("SvrShowFBBindPage", {});
    end;

    ShowFeedBackTips = function(bShow)
        local is_qq_login = 
            TLoginModule.GetLoginAccountType() == TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin;
        if nil ~= TLoginError.pFeedBackTips then
            if bShow and is_qq_login then
                local sTemp = U8toCUIStr(LoadStr("STR_LOGIN_FAILED_FEEDBACK"));
                TLoginError.pFeedBackTips:Clear();
                TLoginError.pFeedBackTips:PushBackHtmlString(sTemp);
                TLoginError.pFeedBackTips:Show(TRUE);
            else
                TLoginError.pFeedBackTips:Show(FALSE);
            end
        end;
    end;

    -- 显示错误
    ShowError = function(sTitle, sError, bRetry)
        if bRetry then    -- 是否可以尝试重新登录
            if (nil ~= TLoginError.pBtnReload) then
                local reloadText = U8toCUIStr(LoadStr(ID_LOGIN_RELOAD));
                TLoginError.pBtnReload:SetCaption(reloadText);
                TLoginError.pBtnReload:Show(TRUE);
            else
                TGPLog.LogE("LoginE", "Reload button is null.");
            end;

            if (nil ~= TLoginError.pBtnRetry) then
                TLoginError.pBtnRetry:Show(FALSE);
            else
                TGPLog.LogE("LoginE", "Retry button is null.");
            end;
        else
            if (nil ~= TLoginError.pBtnReload) then
                TLoginError.pBtnReload:Show(FALSE);
            else
                TGPLog.LogE("LoginE", "Reload button is null.");
            end;

            if (nil ~= TLoginError.pBtnRetry) then
                TLoginError.pBtnRetry:Show(FALSE);
            else
                TGPLog.LogE("LoginE", "Retry button is null.");
            end;
        end;

        -- 离线登录
        if nil ~= TLoginError.pBtnExitWG then
            if nil ~= TLoginError.nOffline then
                local     sText = U8toCUIStr(LoadStr(ID_LOGIN_OFFLINE_LOGIN));
                TLoginError.pBtnExitWG:SetCaption(sText);
            else
                local     sText = U8toCUIStr(LoadStr(ID_LOGIN_QUIT));
                TLoginError.pBtnExitWG:SetCaption(sText);
            end;
        else
            TGPLog.LogE("LoginE", "Exit button is null.");
        end;

        -- 错误标题
        if nil ~= TLoginError.pLblTitle then
            ui_utility.setproperty_by_id(TLoginError.pLblTitle:GetControlID(), 
                                         "Caption", sTitle);
        else
            TGPLog.LogE("LoginE", "Title is null.");
        end;

        -- 错误提示文字
        if nil ~= TLoginError.pEdtError then
            ui_utility.setproperty_by_id(TLoginError.pEdtError:GetControlID(), 
                                         "Caption", sError);
        else
            TGPLog.LogE("LoginE", "Edit tips is null.");
        end;

        TLoginError.ShowFeedBackTips(true);
    end;

    -- 重试
    OnClkRetry = function(pUILib, nBtnID)
        comm_center.call_service("Svr_LoginRetry", {});    -- 尝试重新登录
    end;

    -- 重新加载
    OnClkReload = function(pUILib, nBtnID)
        comm_center.call_service("Svr_Relogin", {});    -- 重新加载
    end;

    -- 退出
    OnClkExit = function(pUILib, nBtnID)
        if nil ~= TLoginError.nOffline then
            comm_center.call_service("Svr_OfflineLogin", {account_id=tostring(TLoginError.nOffline)});

            local     zData = {};

            if nil ~= TLoginError.nOffline and TLoginModule.IsLoginedAccount(TLoginError.nOffline) then
                zData["result"] = "0";
                TLoginError.RptOffline("undirect_offline_login_suc", zData);
            else
                zData["result"] = "invalid_uin";
                TLoginError.RptOffline("undirect_offline_login_fail", zData);
            end;
        else
            comm_center.call_service("Svr_ExitWeGame", {});
        end;
    end;

    -- 去反馈
    OnClkFeedBack = function(pUILib, nBtnID)
        web_utility.open_new_browser(LoadStr("LOGIN_FAILED_FEEDBACK_URL"), false, true);
    end;

    -- 离线登录上报
    RptOffline = function(sSubAct, zData)
        local     zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick;
        zTemp.str_action = "offline_login"
        zTemp.str_type   = sSubAct;
        zTemp.str_json   = base_utility.table_to_json(zData);
        zTemp.game_id    = 0;

        base_utility.lua_qos_offline_report(zTemp)
        zTemp:delete();
    end;

    on_get_wegame_nobind_info = function (data)
        if nil == data then
            return;
        end
        data["tgp_id"] = TLoginError.wegame_id;
        data["tgp_bind_ticket"] = TLoginError.bind_ticket;
        return data;
    end;

    -- 错误页面
    dwRootID   = 0;
    nOffline   = nil;

    pPnlError  = nil;
    pLblTitle  = nil;
    pEdtError  = nil;
    pBtnReload = nil;
    pBtnExitWG = nil;
    wegame_id  = 0;
    bind_ticket = "";
};